package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.query.AccidentObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("accident")
public class AccidentController {
    @Autowired
    private IAccidentService accidentService;

    @RequiresPermissions("accident:index")
    @PermissionName("理赔主页")
    @RequestMapping("index")
    public String index(@ModelAttribute("qo") AccidentObjectQuery qo) {
        return "accident/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult<Accident> list(@ModelAttribute("qo") AccidentObjectQuery qo) {
        PageResult<Accident> result = accidentService.query(qo);
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public List<Accident> query() {
        return accidentService.getAll();
    }

    @RequiresPermissions("accident:save")
    @PermissionName("理赔单创建")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Accident accident) {
        JsonResult jsonResult = new JsonResult();
        try {
            accidentService.save(accident);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("创建理赔单失败");
        }
        return jsonResult;
    }

    @RequiresPermissions("accident:update")
    @PermissionName("理赔单更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Accident accident) {
        JsonResult jsonResult = new JsonResult();
        try {
            accidentService.update(accident);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("创建理赔单失败");
        }
        return jsonResult;
    }

    @RequiresPermissions("client:changeClaimState")
    @PermissionName("理赔状态改变")
    @RequestMapping("changeClaimState")
    @ResponseBody
    public JsonResult changeClaimState(Accident accident) {
        JsonResult jsonResult = new JsonResult();
        try {
            accidentService.changeClaimState(accident);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("客户审核失败,请重试");
        }
        return jsonResult;
    }


    /**
     * znb
     * 查询案件详情
     *
     */
    @RequestMapping("queryCaseItem")
    @ResponseBody
    public List<Map> queryCaseItem(AccidentObjectQuery qo) {
        return accidentService.queryCaseItem(qo);
    }
}
